package com.zito.health.common.exception;

import com.zito.health.common.enums.BusinessExceptionEnum;
import com.zito.health.common.enums.ResultCode;
import com.zito.health.common.util.StringUtil;
import lombok.Data;

/**
 * @description:
 * @author: gxt
 * @time: 2019/10/12 14:38
 */
@Data
public class BusinessExceptions extends RuntimeException {

    private static final long serialVersionUID = 194906846739586856L;

    protected String code;

    protected String message;

    protected ResultCode resultCode;

    protected Object data;

    public BusinessExceptions() {
        BusinessExceptionEnum exceptionEnum = BusinessExceptionEnum.getByEClass(this.getClass());
        if (exceptionEnum != null) {
            resultCode = exceptionEnum.getResultCode();
            code = exceptionEnum.getResultCode().code().toString();
            message = exceptionEnum.getResultCode().message();
        }

    }

    public BusinessExceptions(String message) {
        this();
        this.message = message;
    }

    public BusinessExceptions(String format, Object... objects) {
        this();
        this.message = StringUtil.formatIfArgs(format, "{}", objects);
    }

    public BusinessExceptions(ResultCode resultCode, Object data) {
        this(resultCode);
        this.data = data;
    }

    public BusinessExceptions(ResultCode resultCode) {
        this.resultCode = resultCode;
        this.code = resultCode.code().toString();
        this.message = resultCode.message();
    }

}
